﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Bids.Core.Common.HC
{
    public class Code
    {

        public static string GetTime()
        {
            string result = DateTime.Now.ToLongTimeString();
            result = result.Replace(" ", "");
            result = result.Replace("\\", "");
            result = result.Replace("-", "");
            result = result.Replace(":", "");

            return result;
        }

        /// <summary>
        /// 生成随机的字母

        /// </summary>
        /// <param name="VcodeNum">生成字母的个数</param>
        /// <param name="flag">flag==0表示生成不重复的,flag==1表示生成重复的</param>
        /// <returns>string</returns>
        public static string RndNum(int VcodeNum, byte flag)
        {
            string Vchar = "0,1,2,3,4,5,6,7,8,9";
            string[] VcArray = Vchar.Split(',');
            string VNum = ""; //由于字符串很短，就不用StringBuilder了

            int temp = -1; //记录上次随机数值，尽量避免生产几个一样的随机数


            //采用一个简单的算法以保证生成随机数的不同

            Random rand = new Random();
            for (int i = 1; i <= VcodeNum; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
                }
                int t = rand.Next(VcArray.Length);
                if (temp != -1 && temp == t && flag == 0)
                {
                    return RndNum(VcodeNum, 0);
                }
                temp = t;
                VNum += VcArray[t];
            }
            return VNum;
        }

    }
}
